#coding='utf8'

import wx
import os
import threading
import checksum

class ChecksumPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        vboxer = wx.BoxSizer(wx.VERTICAL)
        vboxer.Add(self._log_item(), 1, wx.EXPAND|wx.ALL, 5)
        vboxer.Add(self._file_item(), 0, wx.SHAPED|wx.ALIGN_CENTER, 5)
        self.SetSizer(vboxer)
    def _file_item(self):
        self.file_select = wx.Button(self, -1, "Select File")
        self.Bind(wx.EVT_BUTTON, self.OnSelectFile, self.file_select)
        return self.file_select
    def _md5_item(self):
        self.md5_item = wx.StaticText(self, -1, "")
        return self.md5_item
    def _crc32_item(self):
        self.crc32_item = wx.StaticText(self, -1, "")
        return self.crc32_item
    def _sha1_item(self):
        self.sha1 = wx.StaticText(self, -1, "")
        return self.sha1
    def _log_item(self):
        self.log_item = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
        return self.log_item
    def message_log(self, filename, process_size, total_size):
        message = u"%s (%d/%d)%.2f%\n" % (filename, process_size, total_size, (100.0*process_size/total_size))
        wx.CallAfter(self.log_item.AppendText, message)
    def checksum_log(self, filename, checksum_type, checksum_value):
        message = u"%s ( %s: %s )\n" % (filename, checksum_type, checksum_value)
        wx.CallAfter(self.log_item.AppendText, message)
    def OnSelectFile(self, event):
        dlg = wx.FileDialog(
                            self, message="Choose one or more files",
                            defaultDir=os.getcwd(),
                            defaultFile="",
                            wildcard="All files (*.*)|*.*",
                            style=wx.OPEN | wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:
            self.path = dlg.GetPath()
            t = threading.Thread(target=checksum.checksum, args=(self.path, self.checksum_log, self.message_log))
            t.start()
        
class ChecksumFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Checksum calculator', size=(800, 600), style=wx.DEFAULT_FRAME_STYLE)
        self.Center()
        self.panel = ChecksumPanel(self)

def main():
    app = wx.PySimpleApp()
    frame = ChecksumFrame()
    frame.Show()
    app.MainLoop()   

if __name__ == '__main__':
    main()
